package com.zhang.factory.example;

import com.zhang.factory.example.utils.ClassUtils;

import java.util.List;
import java.util.Random;

/**
 * @author ElloeStudy(Sifa Zhang)
 * @create 2022-02-28 20:44
 * To: 真常应物，真常得性，常清常静，常清静矣
 */
public class HumanFactory1 {
    public static Human createHuman(Class c){
        Human human = null;
        try {
            human = (Human)Class.forName(c.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return human;
    }
    // 随机产生人种
    public static Human createHuman(){
        Human human = null;  // 定义一个类型的人类
        List<Class> concreateHumanList = ClassUtils.getAllClassByInterface(Human.class);
        Random random = new Random();
        int rand = random.nextInt(concreateHumanList.size());

        human = createHuman(concreateHumanList.get(rand));

        return human;
    }
}
